﻿/****************************************************************************
*                                                                           *
* Copyright (c) Ionescu M. Marius. All rights reserved(2010).               *
*                                                                           *
****************************************************************************/

// (c) Copyright Romanian Software Company.
// Last Change: 20.10.2009 - by Ionescu Marius.
// All rights reserved.

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Collections;
using System.Data;
using AjaxControlToolkit;

namespace NS_Controls
{
	public class HyperLinkFieldX : BoundField
	{
		public string DataNavigateUrlFormatString
		{
			get { return (string)(this.ViewState["DataNavigateUrlFormatString"] ?? ""); }
			set { this.ViewState["DataNavigateUrlFormatString"] = value; }
		}

		public string Target
		{
			get { return (string)(this.ViewState["Target"] ?? ""); }
			set { this.ViewState["Target"] = value; }
		}

		public string DataTextFormatString
		{
			get { return (string)(this.ViewState["DataTextFormatString"] ?? ""); }
			set { this.ViewState["DataTextFormatString"] = value; }
		}

		protected override void InitializeDataCell(DataControlFieldCell cell, DataControlRowState rowState)
		{
			if (!this.ReadOnly && (0 != (rowState & DataControlRowState.Edit)) || 0 != (rowState & DataControlRowState.Insert))
			{
				TextBox txb = new TextBox();
				txb.ID = "txb";
				txb.ToolTip = this.HeaderText;

				if (!String.IsNullOrEmpty(this.DataField) && 0 != (rowState & DataControlRowState.Edit))
				{
					txb.DataBinding += new EventHandler(HandleDataBinding);
				}

				cell.Controls.Add(txb);

				if (0 != (rowState & DataControlRowState.Insert))
				{
					HandleDataBinding(txb, null);
				}
				
			}
			else if (!String.IsNullOrEmpty(this.DataField))
			{
				HyperLink hl = new HyperLink();
				hl.ID = "hl";
				hl.DataBinding += new EventHandler(HandleDataBinding);
				cell.Controls.Add(hl);
			}
		}

		public override void ExtractValuesFromCell(System.Collections.Specialized.IOrderedDictionary dictionary, DataControlFieldCell cell, DataControlRowState rowState, bool includeReadOnly)
		{
			if (cell.HasControls() && cell.Controls[0] is TextBox)
			{
				TextBox txb = (TextBox)cell.Controls[0];
				if (txb.Text != string.Empty)
					dictionary[this.DataField] = txb.Text;
				else
					dictionary[this.DataField] = System.DBNull.Value;
			}
		}

		private void HandleDataBinding(object sender, EventArgs e)
		{
			Control ctrl = (Control)sender;

			string val = null;
			if (ctrl.NamingContainer != null)
			{
				object rawVal = GetValue(ctrl.NamingContainer);
				val = (null == rawVal ? null : rawVal.ToString());
			}

			if (ctrl is TextBox)
			{
				TextBox txf = (TextBox)ctrl;
				txf.Text = val;
			}

			else if (ctrl is HyperLink)
			{
				HyperLink hl = (HyperLink)ctrl;
				hl.NavigateUrl = this.DataNavigateUrlFormatString;
				hl.Target = this.Target;
				hl.Text = val;
			}
		}
	}
}
